/*******************************************************************************
 * Copyright (c) 2006 CEA List.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     CEA List - initial API and implementation
 *******************************************************************************/
package com.cea.papyrus.diagram.component.helper;

import org.eclipse.draw2d.geometry.Dimension;
import org.eclipse.draw2d.geometry.Point;
import org.eclipse.jface.preference.IPreferenceStore;
import org.eclipse.swt.graphics.FontData;
import org.eclipse.uml2.uml.Component;
import org.eclipse.uml2.uml.Element;
import org.eclipse.uml2.uml.Package;
import org.eclipse.uml2.uml.PackageableElement;
import org.eclipse.uml2.uml.UMLFactory;

import com.cea.papyrus.core.helper.NodeHelper;
import com.cea.papyrus.core.utils.Debug;
import com.cea.papyrus.diagram.component.Activator;
import com.cea.papyrus.diagram.component.IComponentDiagramPropertyValue;
import com.cea.papyrus.diagram.component.preference.ComponentDiagramPreferenceConstants;
import com.cea.papyrus.diagraminterchange2.di2.Di2Factory;
import com.cea.papyrus.diagraminterchange2.di2.Diagram;
import com.cea.papyrus.diagraminterchange2.di2.GraphElement;
import com.cea.papyrus.diagraminterchange2.di2.GraphNode;
import com.cea.papyrus.diagraminterchange2.di2.PapyrusKeyEnumeration;
import com.cea.papyrus.diagraminterchange2.di2.Property;
import com.cea.papyrus.diagraminterchange2.di2.Uml1SemanticModelBridge;


/**
 * Defines management of components.
 * 
 * @author A. Lanusse
 * @since 15 Mai 2007
 */
public class NodeComponentHelper extends NodeHelper{
	
	/**
	 * The channel.
	 */
	private int channel = 203;
	
	///////////////////////////////////////////////////////////////////////////
	// UML Management
	///////////////////////////////////////////////////////////////////////////
	
	/* (non-Javadoc)
	 * @see com.cea.papyrus.common.factories.NodeFactory#createElement(java.lang.String, org.eclipse.uml2.uml.Element)
	 */
	
	@Override
	public org.eclipse.uml2.uml.Element createUMLElement(String name, org.eclipse.uml2.uml.Element umlParent) {
		Debug.debug(this,"Call NodeComponentFactory.createUMLElement()", channel); //$NON-NLS-1$

		// create Component
		org.eclipse.uml2.uml.Component aComponent = UMLFactory.eINSTANCE.createComponent();
		aComponent.setName(name);
		if(umlParent instanceof Component) {
			((Component)umlParent).getPackagedElements().add(aComponent);
			return aComponent;
			}
		if(umlParent instanceof Package) {
		((Package)umlParent).getPackagedElements().add(aComponent);
		return aComponent;
		}

		return aComponent;
	}

	/* (non-Javadoc)
	 * @see com.cea.papyrus.common.factories.NodeFactory#canExecuteCreateElement(org.eclipse.uml2.uml.Element)
	 */

	@Override
	public boolean canCreateUMLElement(Element umlParent) {

		if(umlParent instanceof Component) {
			return true;
		}
		if(umlParent instanceof Package) {
			return true;
		}
		return false;
	}

	/* (non-Javadoc)
	 * @see com.cea.papyrus.common.factories.NodeFactory#addElement(org.eclipse.uml2.uml.Element, org.eclipse.uml2.uml.Element)
	 */

	@Override
	public boolean addUMLElement(Element element, Element umlParent) {
		if(umlParent instanceof Component) {
			return ((Component)umlParent).getPackagedElements().add((PackageableElement)element);
		}
		if(umlParent instanceof Package) {
			return ((Package)umlParent).getPackagedElements().add((PackageableElement) element);
		}
		return false;
	}

	/* (non-Javadoc)
	 * @see com.cea.papyrus.common.factories.NodeFactory#removeElement(org.eclipse.uml2.uml.Element, org.eclipse.uml2.uml.Element)
	 */

	@Override
	public boolean removeUMLElement(Element element, Element umlParent) {

		if(umlParent instanceof Component) {
			return ((Component)umlParent).getPackagedElements().remove(element);
		}
		if(umlParent instanceof Package) {
			return ((Package)umlParent).getPackagedElements().remove(element);
		}
		return false;
	}

	/* (non-Javadoc)
	 * @see com.cea.papyrus.common.factories.NodeFactory#getBaseString()
	 */

	@Override
	public String getBaseString() {
		return "Component_"; //$NON-NLS-1$
	}

/**************************************************************************************************************************
 * 
 * GRAPH MANAGEMENT
 * 
 * ************************************************************************************************************************/

	
	@Override
	public boolean canCreateGraphElement(GraphElement parent) {
		if(parent instanceof Diagram) {
			return true; //this a diagram
		} else if (parent.getSemanticModel()!=null){
			Element parentElt= ((Uml1SemanticModelBridge)parent.getSemanticModel()).getElement();
			if(parentElt!=null){
				if (parentElt instanceof org.eclipse.uml2.uml.Component ){
					return true;
				}
				else if (parentElt instanceof org.eclipse.uml2.uml.Package ){
						return true;
				}
			}
		}
		return false;
	}

	/* (non-Javadoc)
	 * @see com.cea.papyrus.common.factories.NodeFactory#createGraphElement(org.eclipse.uml2.uml.Element, com.cea.papyrus.diagraminterchange2.di2.GraphElement, org.eclipse.draw2d.geometry.Point, org.eclipse.draw2d.geometry.Dimension)
	 */

	@Override
	public GraphNode createGraphElement(Element umlChild, GraphElement parent, Point location, Dimension size) {
		Debug.debug(this,"Call NodeComponentFactory.createGraphElement()", channel); //$NON-NLS-1$

		// creates the graph element
		GraphNode graphNode = super.createGraphElement(umlChild, parent, location, size);
		if(size == null){
			graphNode.setSize(new Dimension(200, 100));
		}
		//createPropertyValue
		Property di2property=Di2Factory.eINSTANCE.createProperty();
		di2property.setKey(PapyrusKeyEnumeration.PAPYRUSEDITPART.getLiteral());
		di2property.setValue(IComponentDiagramPropertyValue.COMPONENTDIAGRAM_NODE_COMPONENT);
		graphNode.getProperty().add(di2property);
		
		// finally, adds the DataType graph element to its parent graph element
		parent.getContained().add(graphNode);
		return graphNode;
	}

	
	/* (non-Javadoc)
	 * @see com.cea.papyrus.common.factories.NodeFactory#setGraphElementPreferences(com.cea.papyrus.diagraminterchange2.di2.GraphElement)
	 */

	@Override
	public void setGraphElementPreferences(GraphElement graphElement) {	
		// Get the preference store
		IPreferenceStore store = Activator.getDefault().getPreferenceStore();
		
		// Set colors and font
		graphElement.setBackgroundColor(ComponentDiagramPreferenceConstants.getPreferenceComponentBackgroundColor(store));
		graphElement.setBorderColor(ComponentDiagramPreferenceConstants.getPreferenceComponentBorderColor(store));
		graphElement.setFontColor(ComponentDiagramPreferenceConstants.getPreferenceComponentFontColor(store));
		FontData[] fontdata = ComponentDiagramPreferenceConstants.getPreferenceComponentFont(store);
		if (fontdata.length>0){
			graphElement.setFontFamily(fontdata[0].getName());
			graphElement.setFontSize(fontdata[0].getHeight());
		}
	}
}